/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 科泰集团教研团队
 * @version 1.0.0
 * @packageName com.ktjiaoyu.service.impl
 * @fileName SysUserServiceImpl.java
 * @createTime 2024/10/1 22:06
 * @lastModify 2024/10/1 22:06
 */
@Service
public class SysUserServiceImpl implements SysUserService{

    private static final Logger LOGGER =
            LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser login(String account, String password) {
        SysUser loginUser = sysUserMapper.getByAccount(account);
        // 如果账号存在且密码相等，则返回查到的登录用户信息
        if(loginUser != null && loginUser.getPassword().equals(password)){
            return loginUser;
        }
        return null;
    }

    @Override
    public Page<SysUser> findPage(SysUser entity, Integer pageNo, Integer pageSize)  throws Exception{
        Page<SysUser> page = null;
        try {
            page = new Page<SysUser>();
            page.setPageSize(pageSize);
            // 查询总数，计算总页数
            Long count = sysUserMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);
            // 查询分页数据
            List<SysUser> data = sysUserMapper.getPage(entity, page.offset(), page.getPageSize());
            page.setData(data);
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl.findPage(SysUser, Integer, Integer)方法出现异常.", e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(SysUser entity) throws Exception {
        try {
            if(entity.getId() == null){
                // 无主键则新增
                return sysUserMapper.insert(entity) > 0;
            }else{
                // 有主键则修改
                return sysUserMapper.update(entity) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl.saveOrUpdate(SysUser)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public SysUser findById(Long id) throws Exception{
        try {
            return sysUserMapper.getById(id);
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl" +
                    ".findById(Long)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public SysUser findByAccount(String account) throws Exception{
        try {
            return sysUserMapper.getByAccount(account);
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl" +
                    ".findByAccount(String)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public boolean removeById(Long id) throws Exception {
        try {
            return sysUserMapper.delete(id) > 0;
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl.delete(Long)方法出现异常.", e);
            throw e;
        }
    }
}
